/**
 * FileName: GoodsServiceImpl
 * Author:   sky
 * Date:     2019/12/27 13:51
 * Description:
 */
package com.run.guifeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysql.jdbc.StringUtils;
import com.run.guifeng.dao.GoodsInfoMapper;
import com.run.guifeng.model.entity.GoodsInfo;
import com.run.guifeng.model.enums.GoodsOrderByEnum;
import com.run.guifeng.model.enums.ServiceResultEnum;
import com.run.guifeng.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author sky
 * @create 2019/12/27
 * @since 1.0.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsInfoMapper goodsMapper;

    @Override
    public List<GoodsInfo> getMallGoodsPage(String goodsName, String goodsStatus, Date start, Date end) {
        return goodsMapper.selectList(Wrappers.<GoodsInfo>lambdaQuery()
                .like(!StringUtils.isNullOrEmpty(goodsName), GoodsInfo::getGoodsName, goodsName)
                .eq(!StringUtils.isNullOrEmpty(goodsStatus), GoodsInfo::getStatus, goodsStatus)
                .gt(start != null, GoodsInfo::getCreateTime, start)
                .lt(end != null, GoodsInfo::getCreateTime, end));

    }

    @Override
    public String saveMallGoods(GoodsInfo goods) {
        if (goodsMapper.insert(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String batchSaveMallGoods(List<GoodsInfo> goodsList) {
        if (!CollectionUtils.isEmpty(goodsList)) {
            for (GoodsInfo info : goodsList) {
                if (goodsMapper.insert(info) <= 0) {
                    return ServiceResultEnum.OPERATE_ERROR.getResult();
                }
            }
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.ERROR.getResult();
    }

    @Override
    public String updateMallGoods(GoodsInfo goods) {
        GoodsInfo temp = goodsMapper.selectById(goods.getId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        goods.setUpdateTime(new Date());
        if (goodsMapper.updateById(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public GoodsInfo getMallGoodsById(Integer id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public String batchUpdateSellStatus(Integer[] ids, int sellStatus) {
        for (int id : ids) {
            GoodsInfo temp = goodsMapper.selectById(id);
            if (temp == null) {
                return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
            }
            temp.setStatus(sellStatus);
            if (goodsMapper.updateById(temp) <= 0) {
                return ServiceResultEnum.DB_ERROR.getResult();
            }
        }
        return ServiceResultEnum.SUCCESS.getResult();
    }

    /**
     * orderBy 为空则 默认排序
     * 为 priceUp 价格小到大排序
     * 为 priceDown 价格从大到小排序
     * 为 new 发布时间倒序
     * 默认按照库存数量从大到小排列
     * <p>
     * controller 解决商品信息太长问题
     *
     * @param keyWord
     * @return
     */
    @Override
    public List<GoodsInfo> searchMallGoods(String categoryId, String keyWord) {

            return longStringDeal(goodsMapper.selectList(Wrappers.<GoodsInfo>lambdaQuery()
                    .like(!StringUtils.isNullOrEmpty(keyWord), GoodsInfo::getGoodsName, keyWord)
                    .or().like(!StringUtils.isNullOrEmpty(keyWord), GoodsInfo::getGoodsIntro, keyWord)
                    .eq(categoryId != null, GoodsInfo::getGoodsCategoryId, categoryId)));
    }

//    public List<GoodsInfo> searchMallGoods(String categoryId, String keyWord) {
//        List<GoodsInfo> list = goodsMapper.selectList(
//                new QueryWrapper<GoodsInfo>()
////                        .like("goods_name", keyWord)
////                        .or()
//                        .like("goods_intro",keyWord)
//        );
//        return list;
//    }

    /**
     * 设置商品名称 28
     * 设置商品简介 30
     *
     * @param list
     * @return
     */
    private List<GoodsInfo> longStringDeal(List<GoodsInfo> list) {
        for (GoodsInfo goodInfo : list) {
            String goodsName = goodInfo.getGoodsName();
            String goodsIntro = goodInfo.getGoodsIntro();
            // 字符串过长导致文字超出的问题
            if (goodsName.length() > 28) {
                goodsName = goodsName.substring(0, 28) + "...";
                goodInfo.setGoodsName(goodsName);
            }
            if (goodsIntro.length() > 30) {
                goodsIntro = goodsIntro.substring(0, 30) + "...";
                goodInfo.setGoodsIntro(goodsIntro);
            }
        }
        return list;
    }
}